package com.lxw.manage.course.controller;

import com.lxw.api.course.CourseControllerApi;
import com.lxw.framework.domain.course.CourseBase;
import com.lxw.framework.domain.course.CourseMarket;
import com.lxw.framework.domain.course.CoursePic;
import com.lxw.framework.domain.course.Teachplan;
import com.lxw.framework.domain.course.ext.CourseInfo;
import com.lxw.framework.domain.course.ext.TeachplanNode;
import com.lxw.framework.domain.course.request.CourseListRequest;
import com.lxw.framework.domain.course.response.CourseCode;
import com.lxw.framework.exception.CustomException;
import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.framework.model.response.QueryResult;
import com.lxw.framework.model.response.ResponseResult;
import com.lxw.manage.course.service.ICourseMarketService;
import com.lxw.manage.course.service.ICourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("course")
public class CourseController implements CourseControllerApi {


    @Autowired
    ICourseService courseService;
    @Autowired
    ICourseMarketService courseMarketService;

    @GetMapping("/teachplan/list/{courseId}")
    @Override
    public TeachplanNode findTeachplanList(@PathVariable String courseId) {
        //非空校验
        if (StringUtils.isEmpty(courseId)){
            throw new CustomException(CourseCode.COURSE_PUBLISH_COURSEIDISNULL);
        }

        TeachplanNode teachplanNode = courseService.findTeachplanList(courseId);

        return teachplanNode;
    }

    @GetMapping("/teachplan/get/{id}")
    @Override
    public Teachplan findTeachplanById(@PathVariable String id) {
        //数据回显
        Teachplan teachplan = courseService.findTeachplanById(id);
        return teachplan;
    }

    @PostMapping("/teachplan/add")
    @Override
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {

        ResponseResult responseResult = courseService.addTeachplan(teachplan);


        return responseResult;
    }

    @PostMapping("/teachplan/edit")
    @Override
    public ResponseResult editTeachplan(@RequestBody Teachplan teachplan) {

        ResponseResult responseResult = courseService.editTeachplan(teachplan);
        return responseResult;
    }


    @DeleteMapping("/teachplan/del/{id}")
    @Override
    public ResponseResult delTeachplan(@PathVariable String id) {
        ResponseResult responseResult = courseService.delTeachplanById(id);
        return responseResult;
    }

    @GetMapping("/coursebase/list/{page}/{size}")
    @Override
    public QueryResponseResult findCourseList(
            @PathVariable Integer page, @PathVariable Integer size,
            @RequestParam(required = false) CourseListRequest courseListRequest) {

        //分页查询 多表查询
        QueryResponseResult courseList = courseService.findCourseList(page, size, courseListRequest);
        //封装结果集

        return courseList;
    }

    @PostMapping("/coursebase/add")
    @Override
    public ResponseResult addCourseBase(@RequestBody CourseBase courseBase) {
        //非空验证
        if (StringUtils.isEmpty(courseBase.getName())){
            throw new CustomException(CommonCode.FAIL);
        }
        if (StringUtils.isEmpty(courseBase.getGrade())){
            throw new CustomException(CommonCode.FAIL);
        }
        if (StringUtils.isEmpty(courseBase.getStudymodel())){
            throw new CustomException(CommonCode.FAIL);
        }

        //调用接口
        ResponseResult responseResult = courseService.addCourseBase(courseBase);
        return responseResult;
    }

    @GetMapping("/coursebase/get/{courseId}")
    @Override
    public CourseBase getCourseFormById(@PathVariable String courseId) {
        if (StringUtils.isEmpty(courseId)){
            throw new CustomException(CommonCode.FAIL);
        }
        CourseBase courseBase = courseService.getCourseFormById(courseId);
        return courseBase;
    }

    @PostMapping("/coursebase/update/{courseId}")
    @Override
    public ResponseResult updateCoursebase(@PathVariable String courseId,
                                           @RequestBody CourseBase courseBase) {
        //非空验证
        if (StringUtils.isEmpty(courseBase.getName())){
            throw new CustomException(CommonCode.FAIL);
        }
        if (StringUtils.isEmpty(courseBase.getGrade())){
            throw new CustomException(CommonCode.FAIL);
        }
        if (StringUtils.isEmpty(courseBase.getStudymodel())){
            throw new CustomException(CommonCode.FAIL);
        }
        ResponseResult responseResult = courseService.updateCoursebase(courseId,courseBase);
        return responseResult;
    }

    @GetMapping("/courseMaket/get/{courseId}")
    @Override
    public CourseMarket getCourseMarketById(@PathVariable String courseId) {
        CourseMarket courseMarket = courseMarketService.getCourseMarketById(courseId);
        return courseMarket;
    }

    @PostMapping("/courseMaket/update/{courseId}")
    @Override
    public ResponseResult updateCourseMarket(@PathVariable String courseId
            , @RequestBody CourseMarket courseMarket) {

        //先查询该课程是否存在
        CourseBase courseBase = courseService.getCourseBaseById(courseId);
        if (courseBase == null){
            throw new CustomException(CommonCode.FAIL);
        }
        courseMarket.setId(courseId);
        //非空校验 业务校验 保存数据
        ResponseResult responseResult = courseMarketService.updateCourseMarket(courseMarket);
        return responseResult;
    }

    @PostMapping("/coursepic/add")
    @Override
    public ResponseResult addCoursePic(@RequestParam String courseId,@RequestParam("pic") String fileId) {

        ResponseResult responseResult = courseService.addCoursePic(courseId,fileId);
        return responseResult;
    }

    @GetMapping("/coursepic/list/{courseId}")
    @Override
    public CoursePic getCoursePicById(@PathVariable String courseId) {

        CoursePic coursePic = courseService.getCoursePicById(courseId);
        return coursePic;
    }

    @DeleteMapping("/coursepic/delete")
    @Override
    public ResponseResult delCoursePic(@RequestParam String courseId) {
        ResponseResult responseResult = courseService.delCoursePic(courseId);
        return responseResult;
    }
}
